<template>
    <div class="content_panel project_modules">
        <div class="content_panel_top">
            <h3 class="panel_title">
                <span>{{ $t('finance.finishedOrder') }}</span>
            </h3>
            <el-form ref="form" :model="form" :rules="rules" :inline="false" class="module_form">
                <el-row>
                    <el-col :span="6">
                        <el-form-item prop="orderId">
                            <el-input v-model="form.orderId" :placeholder="$t('common.innerOrderId')" clearable><!--内部订单号--></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item prop="appName" label="">
                            <select-app-list v-model="form.appName" :placeholder="$t('system.appName')"></select-app-list><!--app名称-->
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item prop="thirdPartyOrderId" label="">
                            <el-input v-model="form.thirdPartyOrderId" :placeholder="$t('common.loanOrderId')" clearable></el-input><!--放款订单号-->
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item prop="userPhone">
                            <el-input v-model="form.userPhone" :placeholder="$t('common.mobileNumber')" clearable><!--手机号码--></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item prop="userId">
                            <el-input v-model="form.userId" placeholder="UserId" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item prop="loanCmplTime" label=""><!--放款时间-->
                            <el-date-picker
                                    style="width:100%"
                                    v-model="form.loanCmplTime"
                                    type="datetimerange"
                                    format="yyyy-MM-dd HH:mm:ss"
                                    value-format="timestamp"
                                    range-separator="-"
                                    :default-time="['00:00:00', '23:59:59']"
                                    :start-placeholder="$t('common.loanStartTime')"
                                    :end-placeholder="$t('common.loanEndTime')"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item prop="returnTime" label=""><!--退回时间-->
                            <el-date-picker
                                    style="width:100%"
                                    v-model="form.returnEndTime"
                                    type="datetimerange"
                                    format="yyyy-MM-dd HH:mm:ss"
                                    value-format="timestamp"
                                    range-separator="-"
                                    :default-time="['00:00:00', '23:59:59']"
                                    :start-placeholder="$t('finance.returnStartTime')"
                                    :end-placeholder="$t('finance.returnEndTime')"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button @click="search" icon="el-icon-search" type="primary">{{ $t('common.search') }}</el-button>
                            <el-button @click="exportExcel" icon="el-icon-upload2" type="primary">{{ $t('common.export') }}</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form><!--el-form end-->
        </div><!--content_panel_top-->
        <div class="module_content">
            <div class="content_panel project_modules">
                <div class="table_list">
                    <el-table ref="table" :data="tableData" style="width: 100%;" :max-height="tableMaxHeight">
                        <el-table-column label="UserId" prop="userId" min-width="120"></el-table-column>
                        <el-table-column :label="$t('common.name')" prop="userName" min-width="160"><!--姓名--></el-table-column>
                        <el-table-column :label="$t('common.mobileNumber')" prop="userPhone" min-width="150"><!--手机号码--></el-table-column>
                        <el-table-column :label="$t('common.innerOrderId')" prop="orderId" min-width="200"><!--内部订单号--></el-table-column>
                        <el-table-column :label="$t('system.appName')" prop="appName" min-width="130"></el-table-column><!--APP名称-->
                        <el-table-column :label="$t('common.loanSerialNumber')" prop="controlNumber" min-width="150"><!--放款序列号--></el-table-column>
                        <el-table-column :label="$t('common.loanOrderId')" prop="thirdPartyOrderId" min-width="200"></el-table-column><!--“放款订单号”改为“支付ID”-->
                        <el-table-column :label="$t('common.contractAmount')" prop="contractAmount" min-width="120">
                            <template slot-scope="{ row }">{{ row.contractAmount | formatCurrency }}</template><!--合同金额-->
                        </el-table-column>
                        <el-table-column :label="$t('common.borrowingCycle')" prop="loanPeriod" min-width="120">
                            <template slot-scope="{ row }">{{ row.loanPeriod }}{{ $t('common.day') }}</template><!--借款周期-->
                        </el-table-column>
                        <el-table-column :label="$t('common.loanFee')" prop="loanFee" min-width="120">
                            <template slot-scope="{ row }">{{ row.loanFee | formatCurrency }}</template><!--放款手续费-->
                        </el-table-column>
                        <el-table-column :label="$t('finance.returnFee')" prop="backFee" min-width="120">
                            <template slot-scope="{ row }">{{ row.backFee | formatCurrency }}</template><!--退回手续费-->
                        </el-table-column>
                        <el-table-column :label="$t('common.repaySerialNumber')" prop="contractNumber" width="200"></el-table-column><!--还款序列号-->
                        <el-table-column :label="$t('common.loanAmount')" prop="loanAmount" min-width="120">
                            <template slot-scope="{ row }">{{ row.loanAmount | formatCurrency }}</template><!--放款金额-->
                        </el-table-column>
                        <el-table-column :label="$t('common.loanCompleteTime')" prop="loanCompletionTime" min-width="180">
                            <template slot-scope="{ row }">{{ row.loanCompletionTime | formatDate('yyyy-MM-dd hh:mm:ss')}}</template><!--放款完成时间-->
                        </el-table-column>
                        <el-table-column :label="$t('common.loanChannel')" prop="channelCompany" min-width="180"><!--放款渠道-->
                            <template slot-scope="{ row }">{{ row.channelCompany | SwitchChannelCompany }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('common.loanType')" prop="withdrawChannel" min-width="180"><!--放款方式-->
                            <template slot-scope="{ row }">{{ row.withdrawChannel | SwitchLoanChannel }}</template>
                        </el-table-column>
                        <el-table-column :label="$t('finance.returnTime')" prop="backCompletionTime" min-width="180">
                            <template slot-scope="{ row }">{{ row.backCompletionTime | formatDate('yyyy-MM-dd hh:mm:ss') }}</template><!--退回时间-->
                        </el-table-column>
                    </el-table>
                    <!--分页组件-->
                    <pagination @pageChange="pageChange" :page="page"></pagination>
                </div><!--module_content-->
            </div>
        </div><!--module_content-->
    </div>
</template>

<script>
    import Pagination from '@/components/pagination';
    import mixin from '@/service/mixin';
    import SelectAppList from '@/components/select-app-list';

    export default {
        name: 'finance-loan-finishedList',
        components: {
            Pagination,
            SelectAppList
        },
        mixins: [mixin],
        data() {
            return {
                form: {
                    orderId: '',
                    userPhone: '',
                    withdrawAccount: '',
                    thirdPartyOrderId: '',
                    userId: '',
                    loanCmplTime: [],
                    returnEndTime: [],
                    appName: '',
                },
                rules: {
                    orderId: [],
                    userPhone: [],
                    withdrawAccount: [],
                    thirdPartyOrderId: [],
                    userId: [],
                    loanCmplTime: [],
                    returnEndTime: [],
                }
            };
        },
        methods: {
            // 获取列表数据
            getTableData(page = this.page) {
                const params = this.fillParams(page);
                this.$api.finance.loan.overList(params).then(res => {
                    const {status, data, error} = res;
                    if ('1' === status && '00000000' === error && data) {
                        this.tableData = data.resultData;
                        this.setPage(data.page, this.page);
                    }
                    this.tableMaxHeight = document.body.clientHeight - 220;
                });
            },
            // 搜索列表数据
            search() {
                this.page.currentPage = 1;
                this.getTableData();
            },
            // 导出列表数据
            exportExcel() {
                const params = this.fillParams();
                const user = this.$storage.get('user');
                const language = this.$storage.getLocal('language');
                params.language = language;
                params.token = user.token;
                const {$apiConf} = this;
                const url = this.$urlUtil.fillParams(params, $apiConf.baseUrl + $apiConf.urlType.finance.loan.overExcel, ['page']);
                this.$openNewBlank(url);
            },
            // 配置查询和导出参数
            fillParams(page = this.page) {
                const form = this.form;
                const params = {
                    orderId: '',
                    userPhone: '',
                    withdrawAccount: '',
                    thirdPartyOrderId: '',
                    userId: '',
                    loanTimeStart: '',
                    loanTimeEnd: '',
                    backCompletionTimeStart: '',
                    backCompletionTimeEnd: '',
                    appName: '',
                    page: {
                        pageNo: page.currentPage,
                        pageSize: this.page.pageSize
                    }
                };
                Object.keys(form).forEach(key => {
                    if (typeof form[key] === 'string' && form[key]) {
                        form[key] = form[key].trim();
                        params[key] = form[key];
                    }
                });
                if (form.loanCmplTime) {
                    params.loanTimeStart = form.loanCmplTime[0];
                    params.loanTimeEnd = form.loanCmplTime[1];
                }
                if (form.returnEndTime) {
                    params.backCompletionTimeStart = form.returnEndTime[0];
                    params.backCompletionTimeEnd = form.returnEndTime[1];
                }
                return params;
            }
        },
        created() {
            this.getTableData();
        }
    }
</script>
<style lang="scss">
    [data-page=finance-loan-finishedList] {
        .el-col {
            padding-left: 10px;
        }
        .el-table .cell, .el-table th > .cell {
            word-break: keep-all;
            word-wrap: break-word;
        }
        .el-tooltip__popper {
            max-width: 300px;
            word-break: break-all;
            background-color: #fff;
            box-shadow: 0px 0px 5px #ccc;
            color: #000;
        }
        .el-date-editor .el-range-separator {
            width: 35px;
        }
    }
</style>
